<template>
  <section v-loading="loading" class="cms-body">
    <div class="cms-list-header">
      <el-button type="primary" icon="el-icon-plus" @click="routerLink('/topic/save','save',0)" v-perms="'/topic/save'">
        添加
      </el-button>
      <el-select v-model="params.recommend" @change="goQuery" v-if="false">
        <el-option label="推荐状态" value=""></el-option>
        <el-option label="不推荐" value="0"></el-option>
        <el-option label="推荐" value="1"></el-option>
      </el-select>
      <el-select v-model="params.isDisplay" @change="goQuery">
        <el-option label="上架状态" value=""></el-option>
        <el-option label="未上架" value="0"></el-option>
        <el-option label="已上架" value="1"></el-option>
      </el-select>
      <cms-input label="专题名称" v-model="params.name"></cms-input>
      <el-button @click="goQuery()">查询</el-button>
    </div>
    <!-- 表格 -->
    <el-table :data="tableData" stripe style="width: 100%" @selection-change="checkIds">
      <el-table-column type="selection" width="35" align="center"></el-table-column>
      <el-table-column prop="id" label="id" width="60" align="center"></el-table-column>
      <el-table-column prop="name" label="专题名称" class-name="topic_size" align="left"></el-table-column>

      <el-table-column prop="contentImg" label="封面图" align="center" width="270">
        <template slot-scope="scope">
          <img-preivew :src="scope.row.titleImg | imgPathFilter"></img-preivew>
        </template>
      </el-table-column>

      <el-table-column prop="priority" label="排列顺序" align="center" width="95">
        <div slot-scope="scope">
          <el-input v-model="scope.row.priority" type="number" class="w50"></el-input>
        </div>
      </el-table-column>

      <el-table-column prop="recommend" label="是否推荐" align="center" width="80" v-if="false">
        <div slot-scope="scope">
          <span v-if="scope.row.recommend==0">不推荐</span>
          <span v-if="scope.row.recommend==1">推荐</span>
        </div>
      </el-table-column>

      <el-table-column prop="recommend" label="是否上架" align="center" width="80">
        <div slot-scope="scope">
          <span v-if="scope.row.isDisplay==0">不上架</span>
          <span v-if="scope.row.isDisplay==1">上架</span>
        </div>
      </el-table-column>

      <el-table-column prop="createTime" label="创建日期" width="125" align="center"></el-table-column>
      <el-table-column label="操作" align="center" width="100">
        <div slot-scope="scope">
          <cms-button type="view" @click.native="lookContents(scope.row.id)"
                      v-perms="'/topic/viewNewsPageList'"></cms-button>
          <cms-button type="edit" @click.native="routerLink('/topic/update','update',scope.row.id)"
                      v-perms="'/topic/update'"></cms-button>
          <cms-button type="delete" @click.native="deleteBatch($api.topicDelete,scope.row.id)"
                      v-perms="'/topic/delete'"></cms-button>
        </div>
      </el-table-column>
    </el-table>
    <!-- 表格底部 -->
    <div class="cms-list-footer">
      <div class="cms-left">
        <el-button :disabled="disabled" @click="priorityBatch($api.topicPriority,ids)" v-perms="'/topic/priority'">保存排序
        </el-button>
        <el-button :disabled="disabled" @click="deleteBatch($api.topicDelete,ids)" v-perms="'/topic/delete'">批量删除
        </el-button>
      </div>
      <!-- cms分页组件 -->
      <cms-pagination :total="total" @change="getPages"></cms-pagination>
    </div>

    <el-dialog title="专题文章" :visible.sync="showDrawerTable" direction="rtl" width="90%" @close="initDrawerParams()">
      <div style="margin-bottom:16px;flex:auto;margin-left: 20px;" class="content-flex-right">
        <el-select @change="goQueryTopic()" v-model="channelId" clearable placeholder="请选择">
          <el-option v-for="item in channelList" :key="item.channelId" :label="item.channelName"
                     :value="item.channelId">
          </el-option>
        </el-select>
        <cms-input label="标题" v-model="title"></cms-input>
        <el-button @click="goQueryTopic()">查询</el-button>
      </div>
      <el-table :data="drawerTableData" stripe @selection-change="checkIdsAndStatus" v-loading="loading">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column prop="id" label="id" width="50" align="center"></el-table-column>
        <el-table-column prop="title" label="标题" align="left" min-width="250px">
          <div slot-scope="scope" @click="cellClick(scope.row)" title="点击标题预览">
            <span class="cms-channel-name" style="color: #999;">[{{scope.row.channelName}}]</span>
            <!--  <a class="link" :href="scope.row.url" :title="scope.row.title" target="_blank">-->
            {{scope.row.title}}
            <!--</a>-->
          </div>
        </el-table-column>
        <!--编辑 -->
        <el-table-column label="编辑" prop="userName" align="center"></el-table-column>
        <!--责编 -->
        <el-table-column label="责编" prop="checkUserName" align="center"></el-table-column>
        <!--发布时间 -->
        <el-table-column label="发布时间" prop="releaseDate" align="center"></el-table-column>
        <!--状态 -->
        <el-table-column label="状态" prop="status" align="center">
          <div slot-scope="scope">
            <span v-if="scope.row.status==0">草稿</span>
            <span v-if="scope.row.status==1">待签发</span>
            <span v-if="scope.row.status==2">已签发</span>
            <span v-if="scope.row.status==3">回收站</span>
          </div>
        </el-table-column>
      </el-table>
      <!-- 表格底部 -->
      <div class="cms-list-footer" style="padding-bottom:100px;">
        <div class="cms-left"></div>
        <cms-pagination :total="drawerTotal" @change="getDrawerPages"></cms-pagination>
      </div>
    </el-dialog>
    <el-dialog title="文章预览" :visible.sync="isShowContent" v-if="isShowContent">
      <content-preview :contentId="previewId"></content-preview>
    </el-dialog>


  </section>
</template>

<script>
  import listMixins from '@/mixins/list'
  import axios from "axios";
  import drawerListMixins from '@/mixins/drawerList'

  export default {
    mixins: [listMixins, drawerListMixins],
    data() {
      return {
        params: {//只需要业务参数
          pageNo: 1,
          pageSize: 10
        },
        showDrawerTable: false,
        channelList: [],
        topicId: "",
        isShowContent: false,
        previewId: '',
        channelId: '',
        title: ''
      };
    },
    methods: {
      priorityBatch(url) { //保存排列循序
        let params = {
          ids: this.ids,
          priorities: []
        }
        for (let i in this.checkedList) {
          params.priorities.push(this.checkedList[i].priority);
        }
        params.priorities = params.priorities.join(',');
        this.$confirm('是否保存?', '提示', {type: "warning"}).then(mes => {
          this.$http.post(url, params).then(res => {
            if (res.code == "200") {
              this.successMessage('操作成功');
              this.getTableData(this.params);
            }
          });
        }).catch(error => {
        });
      },
      cellClick(row) {
        this.previewId = row.id;
        this.isShowContent = true;
      },
      lookContents(topicId) {
        this.showDrawerTable = true;
        this.topicId = topicId;
        let params = {'topicId': topicId}
        this.getChannelList();
        this.initDrawerTableData(this.$api.topicViewNewsPageList, params) //请求列表
      },
      initDrawerParams() {
        this.drawerParams.drawerPageNo = 1;
      },
      goQueryTopic() {
        let params = {'topicId': this.topicId, title: this.title, channelId: this.channelId}
        this.drawerParams.drawerPageNo = 1;
        this.initDrawerTableData(this.$api.topicViewNewsPageList, params) //请求列表
      },
      getChannelList() {
        axios.post(this.$api.getOneLevelChannelList, {}).then(res => {
          if (res.code == '200') {
            this.channelList = res.body;
          }
        })
      },
      goQuery() {
        this.getTableData(this.params);
      }
    },
    created() {
      this.initTableData(this.$api.topicList, this.params);
    }
  };
</script>

<style>
  .topic_size {
    font-size: 14px;
  }
</style>
